package com.ny.service.impl;

import com.ny.mapper.CourseScheduleMapper;
import com.ny.mapper.EmployeeMapper;
import com.ny.domain.CourseScheduleDO;
import com.ny.domain.EmployeeDO;
import com.ny.dto.CourseScheduleDTO;
import com.ny.dto.EmployeeDTO;
import com.ny.query.CourseScheduleQuery;
import com.ny.query.EmployeeQuery;
import com.ny.service.EmployeeService;
import java.lang.Boolean;
import java.lang.Override;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ny.exception.BusinessException;
import java.util.Date;

/**
 * 员工管理的实现
 *
 * @author MSI-NB
 * @date 2025-03-08 13:14:38
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private CourseScheduleMapper courseScheduleMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<EmployeeDO> queryList(int pageNum, int pageSize) {
        List<EmployeeDO> employeeDOList = employeeMapper.queryList(pageNum, pageSize);
        if (CollectionUtils.isEmpty(employeeDOList)) {
            EmployeeDO employeeDO = new EmployeeDO();
            employeeDO.setEmployeeId(1);
            employeeDO.setName("小怡");
            employeeDO.setContactInfo("16666666666");
            employeeDO.setJobTitle("教练");
            employeeDO.setResponsibilities("负责一对一私教教学");
            employeeDOList.add(employeeDO);
        }
        return employeeDOList;
    }

    @Override
    public Boolean addEmployee(EmployeeDTO employeeDTO) {
        if (employeeMapper.selectByName(employeeDTO.getName()) != null) {
            throw new BusinessException("员工已存在");
        }
        EmployeeDO employeeDO = new EmployeeDO();
        employeeDO.setName(employeeDTO.getName());
        employeeDO.setContactInfo(employeeDTO.getContactInfo());
        employeeDO.setJobTitle(employeeDTO.getJobTitle());
        employeeDO.setResponsibilities(employeeDTO.getResponsibilities());
        employeeDO.setCreateBy("admin");
        employeeDO.setCreateTime(new Date());
        int result = employeeMapper.insert(employeeDO);
        return result > 0;
    }

    @Override
    public Boolean deleteEmployee(EmployeeQuery employeeQuery) {
        EmployeeDO employeeDO = employeeMapper.selectById(employeeQuery.getEmployeeId());
        if (employeeDO == null) {
            throw new BusinessException("员工不存在");
        }
        int result = employeeMapper.deleteById(employeeQuery.getEmployeeId());
        return result > 0;
    }

    @Override
    public Boolean updateEmployee(EmployeeDTO employeeDTO) {
        EmployeeDO employeeDO = employeeMapper.selectById(employeeDTO.getEmployeeId());
        if (employeeDO == null) {
            throw new BusinessException("员工不存在");
        }
        employeeDO.setName(employeeDTO.getName());
        employeeDO.setContactInfo(employeeDTO.getContactInfo());
        employeeDO.setJobTitle(employeeDTO.getJobTitle());
        employeeDO.setResponsibilities(employeeDTO.getResponsibilities());
        employeeDO.setUpdateBy("admin");
        employeeDO.setUpdateTime(new Date());
        int result = employeeMapper.updateById(employeeDO);
        return result > 0;
    }

    @Override
    public EmployeeDO getEmployeeInfo(EmployeeQuery employeeQuery) {
        EmployeeDO employeeDO = employeeMapper.selectById(employeeQuery.getEmployeeId());
        if (employeeDO == null) {
            throw new BusinessException("员工不存在");
        }
        return employeeDO;
    }

    @Override
    public Boolean addCourseSchedule(CourseScheduleDTO courseScheduleDTO) {
        CourseScheduleDO courseScheduleDO = new CourseScheduleDO();
        courseScheduleDO.setCourseId(courseScheduleDTO.getCourseId());
        courseScheduleDO.setScheduleDate(courseScheduleDTO.getScheduleDate());
        courseScheduleDO.setStartTime(courseScheduleDTO.getStartTime());
        courseScheduleDO.setEndTime(courseScheduleDTO.getEndTime());
        courseScheduleDO.setCreateBy("admin");
        courseScheduleDO.setCreateTime(new Date());
        if (courseScheduleMapper.selectByCourseIdAndDate(courseScheduleDTO.getCourseId(), courseScheduleDTO.getScheduleDate()) != null) {
            throw new BusinessException("课程时间安排已存在");
        }
        int result = courseScheduleMapper.insert(courseScheduleDO);
        return result > 0;
    }

    @Override
    public Boolean deleteCourseSchedule(CourseScheduleQuery courseScheduleQuery) {
        CourseScheduleDO courseScheduleDO = courseScheduleMapper.selectById(courseScheduleQuery.getScheduleId());
        if (courseScheduleDO == null) {
            throw new BusinessException("课程时间安排不存在");
        }
        int result = courseScheduleMapper.deleteById(courseScheduleQuery.getScheduleId());
        return result > 0;
    }

    @Override
    public Boolean updateCourseSchedule(CourseScheduleDTO courseScheduleDTO) {
        CourseScheduleDO courseScheduleDO = courseScheduleMapper.selectById(courseScheduleDTO.getScheduleId());
        if (courseScheduleDO == null) {
            throw new BusinessException("课程时间安排不存在");
        }
        courseScheduleDO.setCourseId(courseScheduleDTO.getCourseId());
        courseScheduleDO.setScheduleDate(courseScheduleDTO.getScheduleDate());
        courseScheduleDO.setStartTime(courseScheduleDTO.getStartTime());
        courseScheduleDO.setEndTime(courseScheduleDTO.getEndTime());
        courseScheduleDO.setUpdateBy("admin");
        courseScheduleDO.setUpdateTime(new Date());
        int result = courseScheduleMapper.updateById(courseScheduleDO);
        return result > 0;
    }

    @Override
    public CourseScheduleDO getCourseScheduleInfo(CourseScheduleQuery courseScheduleQuery) {
        CourseScheduleDO courseScheduleDO = courseScheduleMapper.selectById(courseScheduleQuery.getScheduleId());
        if (courseScheduleDO == null) {
            throw new BusinessException("课程时间安排不存在");
        }
        return courseScheduleDO;
    }
}
